{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>板温</th>\n",
       "      <th>现场温度</th>\n",
       "      <th>光照强度</th>\n",
       "      <th>转换效率</th>\n",
       "      <th>转换效率A</th>\n",
       "      <th>转换效率B</th>\n",
       "      <th>转换效率C</th>\n",
       "      <th>电压A</th>\n",
       "      <th>电压B</th>\n",
       "      <th>电压C</th>\n",
       "      <th>电流A</th>\n",
       "      <th>电流B</th>\n",
       "      <th>电流C</th>\n",
       "      <th>功率A</th>\n",
       "      <th>功率B</th>\n",
       "      <th>功率C</th>\n",
       "      <th>平均功率</th>\n",
       "      <th>风速</th>\n",
       "      <th>风向</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-19.14</td>\n",
       "      <td>-17.4</td>\n",
       "      <td>34</td>\n",
       "      <td>80.55</td>\n",
       "      <td>106.32</td>\n",
       "      <td>16.98</td>\n",
       "      <td>118.36</td>\n",
       "      <td>729</td>\n",
       "      <td>709</td>\n",
       "      <td>725</td>\n",
       "      <td>1.34</td>\n",
       "      <td>0.22</td>\n",
       "      <td>1.50</td>\n",
       "      <td>976.86</td>\n",
       "      <td>155.98</td>\n",
       "      <td>1087.50</td>\n",
       "      <td>740.11</td>\n",
       "      <td>0.6</td>\n",
       "      <td>272</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-18.73</td>\n",
       "      <td>-17.3</td>\n",
       "      <td>30</td>\n",
       "      <td>99.90</td>\n",
       "      <td>139.00</td>\n",
       "      <td>21.20</td>\n",
       "      <td>139.51</td>\n",
       "      <td>728</td>\n",
       "      <td>717</td>\n",
       "      <td>726</td>\n",
       "      <td>1.55</td>\n",
       "      <td>0.24</td>\n",
       "      <td>1.56</td>\n",
       "      <td>1128.40</td>\n",
       "      <td>172.08</td>\n",
       "      <td>1132.56</td>\n",
       "      <td>811.01</td>\n",
       "      <td>0.8</td>\n",
       "      <td>275</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-17.54</td>\n",
       "      <td>-17.0</td>\n",
       "      <td>41</td>\n",
       "      <td>82.48</td>\n",
       "      <td>114.86</td>\n",
       "      <td>14.91</td>\n",
       "      <td>117.66</td>\n",
       "      <td>731</td>\n",
       "      <td>722</td>\n",
       "      <td>720</td>\n",
       "      <td>1.75</td>\n",
       "      <td>0.23</td>\n",
       "      <td>1.82</td>\n",
       "      <td>1279.25</td>\n",
       "      <td>166.06</td>\n",
       "      <td>1310.40</td>\n",
       "      <td>918.57</td>\n",
       "      <td>1.1</td>\n",
       "      <td>283</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-15.43</td>\n",
       "      <td>-16.6</td>\n",
       "      <td>53</td>\n",
       "      <td>73.98</td>\n",
       "      <td>101.72</td>\n",
       "      <td>15.55</td>\n",
       "      <td>104.67</td>\n",
       "      <td>730</td>\n",
       "      <td>727</td>\n",
       "      <td>726</td>\n",
       "      <td>2.02</td>\n",
       "      <td>0.31</td>\n",
       "      <td>2.09</td>\n",
       "      <td>1474.60</td>\n",
       "      <td>225.37</td>\n",
       "      <td>1517.34</td>\n",
       "      <td>1072.44</td>\n",
       "      <td>0.9</td>\n",
       "      <td>280</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-14.60</td>\n",
       "      <td>-16.3</td>\n",
       "      <td>65</td>\n",
       "      <td>64.62</td>\n",
       "      <td>86.86</td>\n",
       "      <td>13.09</td>\n",
       "      <td>93.92</td>\n",
       "      <td>727</td>\n",
       "      <td>729</td>\n",
       "      <td>728</td>\n",
       "      <td>2.13</td>\n",
       "      <td>0.32</td>\n",
       "      <td>2.30</td>\n",
       "      <td>1548.51</td>\n",
       "      <td>233.28</td>\n",
       "      <td>1674.40</td>\n",
       "      <td>1152.06</td>\n",
       "      <td>1.1</td>\n",
       "      <td>280</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      板温  现场温度  光照强度   转换效率   转换效率A  转换效率B   转换效率C  电压A  电压B  电压C   电流A   电流B  \\\n",
       "0 -19.14 -17.4    34  80.55  106.32  16.98  118.36  729  709  725  1.34  0.22   \n",
       "1 -18.73 -17.3    30  99.90  139.00  21.20  139.51  728  717  726  1.55  0.24   \n",
       "2 -17.54 -17.0    41  82.48  114.86  14.91  117.66  731  722  720  1.75  0.23   \n",
       "3 -15.43 -16.6    53  73.98  101.72  15.55  104.67  730  727  726  2.02  0.31   \n",
       "4 -14.60 -16.3    65  64.62   86.86  13.09   93.92  727  729  728  2.13  0.32   \n",
       "\n",
       "    电流C      功率A     功率B      功率C     平均功率   风速   风向  \n",
       "0  1.50   976.86  155.98  1087.50   740.11  0.6  272  \n",
       "1  1.56  1128.40  172.08  1132.56   811.01  0.8  275  \n",
       "2  1.82  1279.25  166.06  1310.40   918.57  1.1  283  \n",
       "3  2.09  1474.60  225.37  1517.34  1072.44  0.9  280  \n",
       "4  2.30  1548.51  233.28  1674.40  1152.06  1.1  280  "
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#coding=utf-8\n",
    "#读取数据\n",
    "import  pandas as pd\n",
    "import numpy as np\n",
    "import scipy as sp\n",
    "\n",
    "train_data = pd.read_csv('./data/public.train.csv')\n",
    "test_data = pd.read_csv('./data/public.test.csv')\n",
    "submit = pd.read_csv('./data/submit_example.csv')\n",
    "y = train_data['发电量']\n",
    "X = train_data.drop(['ID','发电量'], axis=1)\n",
    "X.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### train数据 头信息，信息描述"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#train_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#train_data.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "#train_data.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### test 数据 头信息，信息描述"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>板温</th>\n",
       "      <th>现场温度</th>\n",
       "      <th>光照强度</th>\n",
       "      <th>转换效率</th>\n",
       "      <th>转换效率A</th>\n",
       "      <th>转换效率B</th>\n",
       "      <th>转换效率C</th>\n",
       "      <th>电压A</th>\n",
       "      <th>电压B</th>\n",
       "      <th>电压C</th>\n",
       "      <th>电流A</th>\n",
       "      <th>电流B</th>\n",
       "      <th>电流C</th>\n",
       "      <th>功率A</th>\n",
       "      <th>功率B</th>\n",
       "      <th>功率C</th>\n",
       "      <th>平均功率</th>\n",
       "      <th>风速</th>\n",
       "      <th>风向</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>9</td>\n",
       "      <td>-19.33</td>\n",
       "      <td>-17.5</td>\n",
       "      <td>13</td>\n",
       "      <td>198.32</td>\n",
       "      <td>259.11</td>\n",
       "      <td>42.17</td>\n",
       "      <td>293.66</td>\n",
       "      <td>722</td>\n",
       "      <td>705</td>\n",
       "      <td>721</td>\n",
       "      <td>1.26</td>\n",
       "      <td>0.21</td>\n",
       "      <td>1.43</td>\n",
       "      <td>909.72</td>\n",
       "      <td>148.05</td>\n",
       "      <td>1031.03</td>\n",
       "      <td>696.27</td>\n",
       "      <td>0.3</td>\n",
       "      <td>273</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>13</td>\n",
       "      <td>-16.68</td>\n",
       "      <td>-16.6</td>\n",
       "      <td>50</td>\n",
       "      <td>73.59</td>\n",
       "      <td>97.95</td>\n",
       "      <td>14.70</td>\n",
       "      <td>108.12</td>\n",
       "      <td>729</td>\n",
       "      <td>715</td>\n",
       "      <td>729</td>\n",
       "      <td>1.83</td>\n",
       "      <td>0.28</td>\n",
       "      <td>2.02</td>\n",
       "      <td>1334.07</td>\n",
       "      <td>200.20</td>\n",
       "      <td>1472.58</td>\n",
       "      <td>1002.28</td>\n",
       "      <td>0.9</td>\n",
       "      <td>277</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>17</td>\n",
       "      <td>-13.27</td>\n",
       "      <td>-16.2</td>\n",
       "      <td>83</td>\n",
       "      <td>75.36</td>\n",
       "      <td>73.55</td>\n",
       "      <td>73.36</td>\n",
       "      <td>79.16</td>\n",
       "      <td>728</td>\n",
       "      <td>723</td>\n",
       "      <td>724</td>\n",
       "      <td>2.31</td>\n",
       "      <td>2.32</td>\n",
       "      <td>2.50</td>\n",
       "      <td>1681.68</td>\n",
       "      <td>1677.36</td>\n",
       "      <td>1810.00</td>\n",
       "      <td>1723.01</td>\n",
       "      <td>0.7</td>\n",
       "      <td>280</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>18</td>\n",
       "      <td>-12.41</td>\n",
       "      <td>-16.2</td>\n",
       "      <td>86</td>\n",
       "      <td>76.06</td>\n",
       "      <td>75.89</td>\n",
       "      <td>73.95</td>\n",
       "      <td>78.34</td>\n",
       "      <td>727</td>\n",
       "      <td>729</td>\n",
       "      <td>727</td>\n",
       "      <td>2.48</td>\n",
       "      <td>2.41</td>\n",
       "      <td>2.56</td>\n",
       "      <td>1802.96</td>\n",
       "      <td>1756.89</td>\n",
       "      <td>1861.12</td>\n",
       "      <td>1806.99</td>\n",
       "      <td>1.0</td>\n",
       "      <td>279</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   ID     板温  现场温度  光照强度    转换效率   转换效率A  转换效率B   转换效率C  电压A  电压B  电压C   电流A  \\\n",
       "0   1   0.01   0.1     1    0.00    0.00   0.00    0.00    0    0    0  0.00   \n",
       "1   9 -19.33 -17.5    13  198.32  259.11  42.17  293.66  722  705  721  1.26   \n",
       "2  13 -16.68 -16.6    50   73.59   97.95  14.70  108.12  729  715  729  1.83   \n",
       "3  17 -13.27 -16.2    83   75.36   73.55  73.36   79.16  728  723  724  2.31   \n",
       "4  18 -12.41 -16.2    86   76.06   75.89  73.95   78.34  727  729  727  2.48   \n",
       "\n",
       "    电流B   电流C      功率A      功率B      功率C     平均功率   风速   风向  \n",
       "0  0.00  0.00     0.00     0.00     0.00     0.00  0.1    1  \n",
       "1  0.21  1.43   909.72   148.05  1031.03   696.27  0.3  273  \n",
       "2  0.28  2.02  1334.07   200.20  1472.58  1002.28  0.9  277  \n",
       "3  2.32  2.50  1681.68  1677.36  1810.00  1723.01  0.7  280  \n",
       "4  2.41  2.56  1802.96  1756.89  1861.12  1806.99  1.0  279  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 8409 entries, 0 to 8408\n",
      "Data columns (total 20 columns):\n",
      "ID       8409 non-null int64\n",
      "板温       8409 non-null float64\n",
      "现场温度     8409 non-null float64\n",
      "光照强度     8409 non-null int64\n",
      "转换效率     8409 non-null float64\n",
      "转换效率A    8409 non-null float64\n",
      "转换效率B    8409 non-null float64\n",
      "转换效率C    8409 non-null float64\n",
      "电压A      8409 non-null int64\n",
      "电压B      8409 non-null int64\n",
      "电压C      8409 non-null int64\n",
      "电流A      8409 non-null float64\n",
      "电流B      8409 non-null float64\n",
      "电流C      8409 non-null float64\n",
      "功率A      8409 non-null float64\n",
      "功率B      8409 non-null float64\n",
      "功率C      8409 non-null float64\n",
      "平均功率     8409 non-null float64\n",
      "风速       8409 non-null float64\n",
      "风向       8409 non-null int64\n",
      "dtypes: float64(14), int64(6)\n",
      "memory usage: 1.3 MB\n"
     ]
    }
   ],
   "source": [
    "test_data.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>板温</th>\n",
       "      <th>现场温度</th>\n",
       "      <th>光照强度</th>\n",
       "      <th>转换效率</th>\n",
       "      <th>转换效率A</th>\n",
       "      <th>转换效率B</th>\n",
       "      <th>转换效率C</th>\n",
       "      <th>电压A</th>\n",
       "      <th>电压B</th>\n",
       "      <th>电压C</th>\n",
       "      <th>电流A</th>\n",
       "      <th>电流B</th>\n",
       "      <th>电流C</th>\n",
       "      <th>功率A</th>\n",
       "      <th>功率B</th>\n",
       "      <th>功率C</th>\n",
       "      <th>平均功率</th>\n",
       "      <th>风速</th>\n",
       "      <th>风向</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>8409.000000</td>\n",
       "      <td>8409.000000</td>\n",
       "      <td>8409.000000</td>\n",
       "      <td>8409.000000</td>\n",
       "      <td>8409.000000</td>\n",
       "      <td>8409.000000</td>\n",
       "      <td>8409.000000</td>\n",
       "      <td>8409.000000</td>\n",
       "      <td>8409.000000</td>\n",
       "      <td>8409.000000</td>\n",
       "      <td>8409.000000</td>\n",
       "      <td>8409.000000</td>\n",
       "      <td>8409.000000</td>\n",
       "      <td>8409.000000</td>\n",
       "      <td>8409.000000</td>\n",
       "      <td>8409.000000</td>\n",
       "      <td>8409.000000</td>\n",
       "      <td>8409.000000</td>\n",
       "      <td>8409.000000</td>\n",
       "      <td>8409.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>9134.017362</td>\n",
       "      <td>6.743565</td>\n",
       "      <td>-0.532370</td>\n",
       "      <td>340.494232</td>\n",
       "      <td>67.180684</td>\n",
       "      <td>61.592292</td>\n",
       "      <td>71.052603</td>\n",
       "      <td>68.897216</td>\n",
       "      <td>692.506243</td>\n",
       "      <td>759.009989</td>\n",
       "      <td>712.268284</td>\n",
       "      <td>4.120589</td>\n",
       "      <td>4.457701</td>\n",
       "      <td>4.903527</td>\n",
       "      <td>2891.299515</td>\n",
       "      <td>3396.516889</td>\n",
       "      <td>3131.409281</td>\n",
       "      <td>3139.741955</td>\n",
       "      <td>2.391366</td>\n",
       "      <td>223.006422</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>5135.024213</td>\n",
       "      <td>11.733794</td>\n",
       "      <td>70.730474</td>\n",
       "      <td>220.139546</td>\n",
       "      <td>927.594047</td>\n",
       "      <td>2022.687363</td>\n",
       "      <td>1311.881379</td>\n",
       "      <td>1350.963592</td>\n",
       "      <td>1413.713947</td>\n",
       "      <td>2545.796157</td>\n",
       "      <td>1870.362932</td>\n",
       "      <td>2.570118</td>\n",
       "      <td>14.264229</td>\n",
       "      <td>21.105298</td>\n",
       "      <td>8837.131405</td>\n",
       "      <td>17642.610316</td>\n",
       "      <td>12662.028495</td>\n",
       "      <td>7941.118597</td>\n",
       "      <td>1.646400</td>\n",
       "      <td>96.326689</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>-23.890000</td>\n",
       "      <td>-6414.200000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>4732.000000</td>\n",
       "      <td>-1.790000</td>\n",
       "      <td>-7.500000</td>\n",
       "      <td>155.000000</td>\n",
       "      <td>20.400000</td>\n",
       "      <td>20.380000</td>\n",
       "      <td>20.220000</td>\n",
       "      <td>20.530000</td>\n",
       "      <td>641.000000</td>\n",
       "      <td>638.000000</td>\n",
       "      <td>637.000000</td>\n",
       "      <td>1.740000</td>\n",
       "      <td>1.750000</td>\n",
       "      <td>1.810000</td>\n",
       "      <td>1181.040000</td>\n",
       "      <td>1176.600000</td>\n",
       "      <td>1233.690000</td>\n",
       "      <td>1225.620000</td>\n",
       "      <td>1.200000</td>\n",
       "      <td>167.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>9262.000000</td>\n",
       "      <td>5.560000</td>\n",
       "      <td>-2.300000</td>\n",
       "      <td>312.000000</td>\n",
       "      <td>25.060000</td>\n",
       "      <td>24.770000</td>\n",
       "      <td>24.800000</td>\n",
       "      <td>25.380000</td>\n",
       "      <td>668.000000</td>\n",
       "      <td>666.000000</td>\n",
       "      <td>666.000000</td>\n",
       "      <td>4.040000</td>\n",
       "      <td>4.130000</td>\n",
       "      <td>4.100000</td>\n",
       "      <td>2710.500000</td>\n",
       "      <td>2762.540000</td>\n",
       "      <td>2756.000000</td>\n",
       "      <td>2739.600000</td>\n",
       "      <td>2.200000</td>\n",
       "      <td>269.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>13576.000000</td>\n",
       "      <td>15.470000</td>\n",
       "      <td>8.300000</td>\n",
       "      <td>529.000000</td>\n",
       "      <td>36.960000</td>\n",
       "      <td>36.060000</td>\n",
       "      <td>36.630000</td>\n",
       "      <td>36.930000</td>\n",
       "      <td>689.000000</td>\n",
       "      <td>686.000000</td>\n",
       "      <td>686.000000</td>\n",
       "      <td>6.470000</td>\n",
       "      <td>6.530000</td>\n",
       "      <td>6.610000</td>\n",
       "      <td>4245.750000</td>\n",
       "      <td>4262.500000</td>\n",
       "      <td>4324.900000</td>\n",
       "      <td>4268.160000</td>\n",
       "      <td>3.300000</td>\n",
       "      <td>283.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>17875.000000</td>\n",
       "      <td>36.800000</td>\n",
       "      <td>54.500000</td>\n",
       "      <td>934.000000</td>\n",
       "      <td>61448.540000</td>\n",
       "      <td>183289.380000</td>\n",
       "      <td>86857.130000</td>\n",
       "      <td>77396.680000</td>\n",
       "      <td>65477.000000</td>\n",
       "      <td>65508.000000</td>\n",
       "      <td>65514.000000</td>\n",
       "      <td>9.570000</td>\n",
       "      <td>653.710000</td>\n",
       "      <td>652.040000</td>\n",
       "      <td>453360.600000</td>\n",
       "      <td>603301.050000</td>\n",
       "      <td>458325.000000</td>\n",
       "      <td>202906.540000</td>\n",
       "      <td>25.200000</td>\n",
       "      <td>619.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 ID           板温         现场温度         光照强度          转换效率  \\\n",
       "count   8409.000000  8409.000000  8409.000000  8409.000000   8409.000000   \n",
       "mean    9134.017362     6.743565    -0.532370   340.494232     67.180684   \n",
       "std     5135.024213    11.733794    70.730474   220.139546    927.594047   \n",
       "min        1.000000   -23.890000 -6414.200000     0.000000      0.000000   \n",
       "25%     4732.000000    -1.790000    -7.500000   155.000000     20.400000   \n",
       "50%     9262.000000     5.560000    -2.300000   312.000000     25.060000   \n",
       "75%    13576.000000    15.470000     8.300000   529.000000     36.960000   \n",
       "max    17875.000000    36.800000    54.500000   934.000000  61448.540000   \n",
       "\n",
       "               转换效率A         转换效率B         转换效率C           电压A           电压B  \\\n",
       "count    8409.000000   8409.000000   8409.000000   8409.000000   8409.000000   \n",
       "mean       61.592292     71.052603     68.897216    692.506243    759.009989   \n",
       "std      2022.687363   1311.881379   1350.963592   1413.713947   2545.796157   \n",
       "min         0.000000      0.000000      0.000000      0.000000      0.000000   \n",
       "25%        20.380000     20.220000     20.530000    641.000000    638.000000   \n",
       "50%        24.770000     24.800000     25.380000    668.000000    666.000000   \n",
       "75%        36.060000     36.630000     36.930000    689.000000    686.000000   \n",
       "max    183289.380000  86857.130000  77396.680000  65477.000000  65508.000000   \n",
       "\n",
       "                电压C          电流A          电流B          电流C            功率A  \\\n",
       "count   8409.000000  8409.000000  8409.000000  8409.000000    8409.000000   \n",
       "mean     712.268284     4.120589     4.457701     4.903527    2891.299515   \n",
       "std     1870.362932     2.570118    14.264229    21.105298    8837.131405   \n",
       "min        0.000000     0.000000     0.000000     0.000000       0.000000   \n",
       "25%      637.000000     1.740000     1.750000     1.810000    1181.040000   \n",
       "50%      666.000000     4.040000     4.130000     4.100000    2710.500000   \n",
       "75%      686.000000     6.470000     6.530000     6.610000    4245.750000   \n",
       "max    65514.000000     9.570000   653.710000   652.040000  453360.600000   \n",
       "\n",
       "                 功率B            功率C           平均功率           风速           风向  \n",
       "count    8409.000000    8409.000000    8409.000000  8409.000000  8409.000000  \n",
       "mean     3396.516889    3131.409281    3139.741955     2.391366   223.006422  \n",
       "std     17642.610316   12662.028495    7941.118597     1.646400    96.326689  \n",
       "min         0.000000       0.000000       0.000000     0.000000     0.000000  \n",
       "25%      1176.600000    1233.690000    1225.620000     1.200000   167.000000  \n",
       "50%      2762.540000    2756.000000    2739.600000     2.200000   269.000000  \n",
       "75%      4262.500000    4324.900000    4268.160000     3.300000   283.000000  \n",
       "max    603301.050000  458325.000000  202906.540000    25.200000   619.000000  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_data.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### submit 文件展示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "#submit.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['ID', '板温', '现场温度', '光照强度', '转换效率', '转换效率A', '转换效率B', '转换效率C', '电压A',\n",
       "       '电压B', '电压C', '电流A', '电流B', '电流C', '功率A', '功率B', '功率C', '平均功率', '风速',\n",
       "       '风向', '发电量'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_data.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 最初的版本 2018年7月25日 模仿葡萄酒项目的代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.998196941879672\n",
      "0.021796396898211425\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['rf_regressor.pkl']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2. Import libraries and modules\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    " \n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn import preprocessing\n",
    "from sklearn.ensemble import RandomForestRegressor\n",
    "from sklearn.pipeline import make_pipeline\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.metrics import mean_squared_error, r2_score\n",
    "from sklearn.externals import joblib \n",
    " \n",
    "# 3. Load red wine data.\n",
    "# dataset_url = 'http://mlr.cs.umass.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv'\n",
    "# data = pd.read_csv(dataset_url, sep=';')\n",
    "\n",
    "train_data = pd.read_csv('./public.train.csv')\n",
    "test_data = pd.read_csv('./public.test.csv')\n",
    "submit = pd.read_csv('./submit_example.csv') \n",
    "\n",
    "# 4. Split data into training and test sets\n",
    "y = train_data['发电量']\n",
    "X = train_data.drop(['发电量','ID'], axis=1)\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, \n",
    "                                                    test_size=0.2, \n",
    "                                                    random_state=123, \n",
    "                                                   )\n",
    " \n",
    "# 5. Declare data preprocessing steps\n",
    "pipeline = make_pipeline(preprocessing.StandardScaler(), \n",
    "                         RandomForestRegressor(n_estimators=100))\n",
    " \n",
    "# 6. Declare hyperparameters to tune\n",
    "hyperparameters = { 'randomforestregressor__max_features' : ['auto', 'sqrt', 'log2'],\n",
    "                  'randomforestregressor__max_depth': [None, 5, 3, 1]}\n",
    " \n",
    "# 7. Tune model using cross-validation pipeline\n",
    "clf = GridSearchCV(pipeline, hyperparameters, cv=10)\n",
    " \n",
    "clf.fit(X_train, y_train)\n",
    " \n",
    "# 8. Refit on the entire training set\n",
    "# No additional code needed if clf.refit == True (default is True)\n",
    " \n",
    "# 9. Evaluate model pipeline on test data\n",
    "pred = clf.predict(X_test)\n",
    "print (r2_score(y_test, pred))\n",
    "print (mean_squared_error(y_test, pred))\n",
    " \n",
    "# 10. Save model for future use\n",
    "joblib.dump(clf, 'rf_regressor.pkl')\n",
    "# To load: clf2 = joblib.load('rf_regressor.pkl')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 将test_data的训练数据输入模型，计算出结果，并保存"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "clf = joblib.load(\"rf_regressor.pkl\")\n",
    "\n",
    "df_result = pd.DataFrame()\n",
    "df_result['ID'] = list(test_data['ID'])\n",
    "test_feature = test_data.drop('ID', axis=1)\n",
    "pre = clf.predict(test_feature)\n",
    "\n",
    "df_result['Score'] = pre\n",
    "df_result.to_csv('result/submit.csv', index=False, header=False, float_format='%.8f')\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "测试输出的结果\n",
    "\n",
    "0.9979879496067704\n",
    "\n",
    "0.024322814919608308\n",
    "\n",
    "['rf_regressor.pkl']\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2018年7月28号晚上23点34分 modle 完整版"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "done\n"
     ]
    }
   ],
   "source": [
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.decomposition import PCA\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "\n",
    "from sklearn.pipeline import Pipeline\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    " \n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn import preprocessing\n",
    "from sklearn.ensemble import RandomForestRegressor\n",
    "from sklearn.pipeline import make_pipeline\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.metrics import mean_squared_error, r2_score\n",
    "from sklearn.externals import joblib \n",
    "from sklearn.metrics import mean_squared_error\n",
    "# 3. Load red wine data.\n",
    "# dataset_url = 'http://mlr.cs.umass.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv'\n",
    "# data = pd.read_csv(dataset_url, sep=';')\n",
    "\n",
    "train_data = pd.read_csv('./data/public.train.csv')\n",
    "test_data = pd.read_csv('./data/public.test.csv')\n",
    "submit = pd.read_csv('./data/submit_example.csv') \n",
    "\n",
    "# 4. Split data into training and test sets\n",
    "y = train_data['发电量']\n",
    "X = train_data.drop(['ID','发电量'], axis=1)\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, \n",
    "                                                    test_size=0.2, \n",
    "                                                    random_state=123, \n",
    "                                                   )\n",
    "# clf = RandomForestRegressor(n_estimators=100,n_jobs=-1,max_features=\"sqrt\",max_depth=None)\n",
    "# clf = Pipeline([('sc', StandardScaler()),\n",
    "#                     ('pca', PCA(n_components=15)),\n",
    "#                     ('clf', RandomForestRegressor(n_estimators=100,n_jobs=-1,max_features=\"sqrt\",max_depth=None))\n",
    "#                     ])\n",
    "clf = Pipeline([\n",
    "                    ('clf', RandomForestRegressor(n_estimators=100,n_jobs=-1,max_features=\"sqrt\",max_depth=None))\n",
    "                    ])\n",
    "clf.fit(X_train, y_train)\n",
    "y_pred = clf.predict(X_test)\n",
    "mean_squared_error(y_pred, y_test)\n",
    "\n",
    "# 10. Save model for future use\n",
    "joblib.dump(clf, './model_save/rf_regressor_7_28_23_33.pkl')\n",
    "\n",
    "# predict and get result\n",
    "\n",
    "df_result = pd.DataFrame()\n",
    "df_result['ID'] = list(test_data['ID'])\n",
    "test_feature = test_data.drop('ID', axis=1)\n",
    "pre = clf.predict(test_feature)\n",
    "\n",
    "df_result['Score'] = pre\n",
    "df_result.to_csv('result/submit_7_28_23_33.csv', index=False, header=False, float_format='%.8f')\n",
    "print(\"done\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 纯RandomForestRegressor 无标准化\n",
    "0.02040348248160798\n",
    "```\n",
    "clf = RandomForestRegressor(n_estimators=100,n_jobs=-1,max_features=\"sqrt\",max_depth=None)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 没有pca的代码\n",
    "0.022231228381953298\n",
    "```\n",
    "clf = Pipeline([('sc', StandardScaler()),\n",
    "#                     ('pca', PCA(n_components=15)),\n",
    "                    ('clf', RandomForestRegressor(n_estimators=100,n_jobs=-1,max_features=\"sqrt\",max_depth=None))\n",
    "                    ])\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 标准化+PCA_15+RandomForestRegressor\n",
    "0.05047384637691991 \n",
    "```\n",
    "clf = Pipeline([('sc', StandardScaler()),\n",
    "                    ('pca', PCA(n_components=15)),\n",
    "                    ('clf', RandomForestRegressor(n_estimators=100,n_jobs=-1,max_features=\"sqrt\",max_depth=None))\n",
    "                    ])\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "clf.best_params_\n",
    "\n",
    "{'randomforestregressor__max_depth': None, 'randomforestregressor__max_features': 'sqrt'}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_result = pd.DataFrame()\n",
    "df_result['ID'] = list(test_all['ID'])\n",
    "df_result['Score'] = pre\n",
    "df_result.to_csv('result/lgb_result.csv', index=False, header=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
